package org.readium.r2.testapp

import android.content.Context
import android.content.SharedPreferences
import androidx.lifecycle.ViewModel
import androidx.lifecycle.MutableLiveData
import android.app.Application

class UserViewModel(application: Application) : ViewModel() {
    private val preferences: SharedPreferences =
        application.getSharedPreferences("app_prefs", Context.MODE_PRIVATE)
    val userName: MutableLiveData<String> = MutableLiveData(
        preferences.getString("nickname", "HUST") // 默认昵称为 "HUST"
    )

    fun updateNickname(newNickname: String) {
        preferences.edit().putString("nickname", newNickname).apply() // 保存昵称到 SharedPreferences
        userName.value = newNickname // 更新 LiveData 中的昵称
    }
}